
// 页码相关
let page = {
  total: 1,
  startIndex: 0,
  stepNumber: 40,
  currentPage: 1
};

// 页面的数据源
let data = {
  couponModelList: [],
  roleList:[],
  roleTreeData:[]
};
let status = {
  //新建或编辑的弹出框
  modalModify: {
    isShow: false,
    isModify: false,
    title: "优惠券模板",
    isLoading: true,
    isOnlyShow : false //是否只用于查看模版详情
  },
  modalAction:{
    isShow: false,
    isModify: false,
    type:0,
    title: '改变模板状态',
    isLoading: true,
    id:'',
    content:"",
    name:""
  },
  rules: {
    baseRules:{
      title: [
        {required: true, message: '标题不能为空', trigger: 'blur'}
      ],
      distributionMethod:[
        {required: true, type:"number",message: '发放方式不能为空', trigger:'change'}
      ],
      // discountRate:[
      //   {
      //     validator: function (rule, value, callback) {
      //       console.log(value);
      //       if (value <=0) {
      //         callback(new Error('折扣比例和优惠券面值需大于0'));
      //       } else {
      //         callback();
      //       }
      //     },
      //     trigger: "blur"
      //   }
      // ],
      // faceValue:[
      //   {
      //     validator: function (rule, value, callback) {
      //       console.log(value);
      //       if (value <=0) {
      //         callback(new Error('折扣比例和优惠券面值需大于0'));
      //       } else {
      //         callback();
      //       }
      //     },
      //     trigger: "blur"
      //   }
      // ],
      validityPeriod:[
        {
          type:'number',
          validator: function (rule, value, callback) {
            let periodReg = /^\d+$/;
            if(params.couponModel.validityPeriodType == 1 && (!periodReg.test(value) || !value)){
              callback(new Error('有效期必须大于0'));
            } else {
              callback();
            }
          },
          trigger: "blur,change"
        }
        ],
      threshold:[
        {required: true,type:'number', message: '请填写使用门槛', trigger: 'blur,change'}
      ],
      uniqueType:[
        {required: true, type:"number", message: '请选择唯一性校验方式', trigger: 'change'}
      ]
    },

  }
};


let table =function(self){ return [
  {
    type: 'index',
    width: 55,
    fixed: 'left'
  },
  {
    title: '优惠券标题',
    key: 'title',
    width: 200,
    render:(h, params) => {
      if(self.$authority("COUPONS-1-204")){
        return (<div><a href="javascript:void(0);" onClick={() => {self.showCouponModelInfo(params.row.id);}}>{params.row.title}</a></div>);
      } else {
        return (<div>{params.row.title}</div>);
      }
    }
  },
  {
    title: '优惠券类型',
    key: 'type',
    width: 120,
    align: 'center',
    render: (h, params) => {
      let couponTypeDict = self.couponTypeDict;
      let type = params.row.type;
      let typeItem = couponTypeDict.filter(item=>item.value===type);
      let isPrimary = type===200;
      return (
        <div>
          <tag color={isPrimary?'green':'magenta'}>{isPrimary?'折扣券':'现金抵扣券'}</tag>
        </div>
      );
    },
  },
  {
    title: '启用状态',
    key: 'state',
    width: 110,
    render: (h, params) => {

      let isOpened = params.row.state === 0;
      let statusName = isOpened?'生效中':'已禁用';
      return (
        <div>
          <badge status={isOpened?'processing':'default'} text={statusName} />
        </div>
      );
    },
    align: 'center'
  },
  {
    title: '发放方式',
    key: 'state',
    width: 110,
    render: (h, params) => {
        return h('div',  params.row.distributionMethod===100?'用户兑换':'后台发放');
    },
    align: 'center'
  },
  {
    title: '折扣',
    key: 'discount',
    align: 'center',
    width: 180,
    render: (h, params) => {
      let discountValue = "";
      let couponType = params.row.type;
      if(couponType===100){
        discountValue = '$'+params.row.faceValue;
      }else if(couponType == 200){
        discountValue = (100-params.row.discountRate*100).toFixed(1)+"%";
      }
      return h('div','满 $'+params.row.threshold+' 减 '+ discountValue)
    }
  },
  {
    title: '有效期',
    key: 'validityPeriod',
    width: 200,
    align: 'center',
    render: (h, params) => {
      switch(params.row.validityPeriodType){
        case 1:
          if(params.row.effectiveTime){ //有生效时间
            let days = 0;
            let now = new Date().getTime();
            if(params.row.effectiveTime > now){ //还没到生效时间
              days = (params.row.validityPeriod/24/3600/1000).toFixed(0);
              return(
                <div>
                  <p style="color:#757575;font-size:0.8em">$tiyBJTime(params.row.effectiveTime)}生效</p>
                  <span>有效期{days}天</span>
                </div>
              );
            }
            let endTimeStamp = params.row.validityPeriod+params.row.effectiveTime;
            if (endTimeStamp < now){
              return h('strong','已过期');
            }

            let endTime = self.$tiyBJTime(endTimeStamp);
            days = ((endTimeStamp-now)/24/3600/1000).toFixed(0);
            return(
              <div>
                <p style="color:#757575;font-size:0.8em">{endTime}失效</p>
                <span>仅剩{days}天</span>
              </div>
            );
          } else { //即时起效
            let days = (params.row.validityPeriod /24/3600/1000).toFixed(0);
            return (<div><span>获取后{days}天内有效</span></div>);
          }
          break;
        case 2:
          return (<div><span>当月有效</span></div>);
          break;
        default :
          return (<div></div>);
      }
    }
  },
  {
    title: '综合操作',
    key: 'action',
    align: 'center',
    fixed: self.$yMobile()?'':'right',
    width: 300,
    render: (h, params) => {
      return h('div', [
        self.$authority("COUPONS-1-201")?h('Button', {
          props: {type: 'primary', size: 'small', icon: "md-create",},
          style: {marginRight: '8px', marginTop: '6px', marginBottom: '6px', },
          on: {
            click: () => {
              self.editCouponModel(params.row.id)
            }
          }
        }, "编辑"):"",
        self.$authority("COUPONS-1-202")?h('Button', {
          props: {type: 'info', size: 'small', icon: "md-flash",},
          style: {marginRight: '8px', marginTop: '6px', marginBottom: '6px',},
          on: {
            click: () => {
              self.showCouponModelAction(params.row.id,params.row.state, params.row.title);
            }
          }
        }, params.row.state==0?'禁用':'启用'):"",
        self.$authority("COUPONS-1-203")?h('Button', {
          props: {type: 'error', size: 'small', icon: "md-trash",},
          style: {marginRight: '8px', marginTop: '6px', marginBottom: '6px',},
          on: {
            click: () => {
              self.deleteCouponModel(params.row.id, params.row.title);
            }
          }
        }, "删除"):""
      ])
    }

  }
]
}

// 和网络交互相关的参数
let params = {
  //搜索条件
  couponType: '',
  couponName: '',
  couponId:'',
  couponModel:{
    id:'',
    name:'',
    title:'',
    type:100,
    state:0,
    distributionMethod:'',
    discountRate:0,
    faceValue:0,
    validityPeriod:0,
    distributionRoles:[],
    threshold:0,
    couponCode:[],
    effectiveTime:'',
    uniqueType:-1,
    validityPeriodType:1
  }
};

export  default  {
  tPage: page,
  tParam: params,
  tStatus:status,
  tData: data,
  tTable: table
};

